/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.RatingDao;
import java.util.List;
import model.Rating;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import util.HibernateUtil;

/**
 *
 * @author rafael
 */
public class RatingDaoImpl implements RatingDao {

    @Override
    public List<Rating> getRatingVeiculos(Integer id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Rating.class);
        criteria.add(Restrictions.eq("veiculos.pkVeiculos", id));
        try {
            return criteria.list();
        } catch (HibernateException hibernateException) {
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public void inserir(Rating rating) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(rating);
            session.beginTransaction().commit();            
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage("Erro", "Não foi possível avaliar!");
            System.out.println(e.getLocalizedMessage());
        } finally {
            session.close();
        }
    }

    @Override
    public Rating getRatingUsuario(Integer idUser, Integer idVeiculo) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Rating.class);
        criteria.add(Restrictions.eq("usuarios.pkId", idUser));
        criteria.add(Restrictions.eq("veiculos.pkVeiculos", idVeiculo));
        try {
            return (Rating) criteria.uniqueResult();
        } catch (HibernateException hibernateException) {
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public void eliminar(Rating rating) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(rating);
            session.beginTransaction().commit();            
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage("Erro", "Não foi possível eliminar!");
            System.out.println(e.getLocalizedMessage());
        } finally {
            session.close();
        }
    }

    @Override
    public List<Rating> getRatingUsuario(Integer idUser) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Rating.class);
        criteria.add(Restrictions.eq("usuarios.pkId", idUser));
        
        try {
            return criteria.list();
        } catch (HibernateException hibernateException) {
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }
}
